<template>
	<view class="details">
		 <scroll-view scroll-y="true" class="scroll">
		 	<!-- 轮播图 -->
		 	<swiper class="swiper" circular indicator-dots autoplay interval="2000" indicator-color indicator-active-color>
		 		<swiper-item v-for="item,index in swiper" :key="index">
		 			<image :src="item.pics_big" mode="widthFix"></image>
		 		</swiper-item>
		 	</swiper>
		 	<!-- 价格 -->
		 	<view class="price">￥{{msg.goods_price}}</view>
		 	<view class="title">
		 		<view class="title-left">{{msg.goods_name}}</view>
		 		<view class="title-cen">|</view>
		 		<view class="title-rig">收藏</view>
		 	</view>
		 	<!-- 图文详情 -->
		 	<view class="xiang">图文详情</view>
		 	<!--富文本-->
		 	<rich-text :nodes="msg.goods_introduce"></rich-text>
		 </scroll-view>
		<!-- 加入购物车 -->
		<view>
			<uni-goods-nav :options="options" :fill="false" :button-group="buttonGroup" @click="onClick"
				@buttonClick="buttonClick" />
		</view>
	</view>
</template>

<script>
	import api from '../../utils/api.js'
	export default {
		data() {
			return {
				swiper: [],
				msg: [],
				shop: [],
				options: [{
					icon: 'chat',
					text: '客服'
				}, {
					icon: 'shop',
					text: '分享',
					infoBackgroundColor: '#007aff',
					infoColor: "#f5f5f5"
				}, {
					icon: 'cart',
					text: '购物车',
					info: '0'
				}],
				buttonGroup: [{
						text: '加入购物车',
						backgroundColor: 'linear-gradient(90deg, #FFCD1E, #FF8A18)',
						color: '#fff'
					},
					{
						text: '立即购买',
						backgroundColor: 'linear-gradient(90deg, #FE6035, #EF1224)',
						color: '#fff'
					}
				],
			};
		},
		methods: {
			onClick(e) { //左边消息
				if (e.index == 2) {
					uni.switchTab({
						url: '/pages/caty/caty'
					})
				}
			},
			buttonClick(e) { // 加入购物车
				if (e.index == 0) {
					this.options[2].info++
					let obj = {
						flag: false,
						img: this.msg.goods_small_logo,
						num: 1,
						price: this.msg.goods_price,
						name: this.msg.goods_name,
						id:this.msg.goods_id
					}
					this.shop = uni.getStorageSync('cart') || []
					let i = this.shop.findIndex(item=>item.id==obj.id)
					if(i!=-1){
					   this.shop[i].num++
					}else{
						this.shop.push(obj)
					}
					uni.setStorageSync('cart',this.shop)
					this.options[2].info = this.shop.length
					uni.showToast({
						title:'加入购物车成功'
					})
				}
			}
		},
		onLoad(options) {
			let goods_id = options.id
			api.get('goods/detail', {
				goods_id
			}).then(res => {
				// console.log(res);
				this.swiper = res.message.pics
				this.msg = res.message
			})
		}
	}
</script>

<style lang="scss">
   .scroll{
	   height: calc(100vh - 100rpx);
   }
	.xiang {
		color: red;
		font-weight: 900;
		padding: 20rpx;
		margin: 10rpx 0;
	}

	.price {
		color: #eb4450;
		font-size: 45rpx;
		margin-top: 15rpx;
	}

	.title {
		border: 2rpx solid #dedede;
		display: flex;
		align-items: center;

		.title-left {
			width: 75%;
			font-size: 30rpx;
			text-align: center;
			overflow: hidden;
		}

		.title-cen {
			width: 5%;
			color: #dedede;
			font-size: 70rpx;
			text-align: center;
		}

		.title-rig {
			width: 20%;
			text-align: center;
		}
	}
</style>
